﻿using System;
using Macds.Debug.Appender;
using Macds.Debug.Test.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace Macds.Debug.Test
{
    [TestClass]
    public class DebugFactoryTest
    {
        [TestMethod]
        public void Simple_log_test()
        {
            // Initialize LogHolder
            var holder = new LogHolder();

            // Create dynamic proxy class that includes the debug interceptor
            var service = DebugFactory.CreateProxy<ICustomerService>(new CustomerService());

            // Execute GetEmail method
            string email = service.GetEmail(1);

            // Validate the returned value
            Assert.AreEqual("demo@email.com", email);

            // Validate the log created in the holder
            // * the holder is used by the DebugFactory in debug mode to help you
            //   create unit test
            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf("ICustomerService.GetEmail") != -1);
            #endif
        }

        [TestMethod]
        public void Validate_multiple_methods_call()
        {
            // Initialize LogHolder
            var holder = new LogHolder();

            // Create dynamic proxy class that includes the debug interceptor
            var service = DebugFactory.CreateProxy<ICustomerService>(new CustomerService());

            // Execute GetEmail method
            service.GetEmail(1);
            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf("ICustomerService.GetEmail(Int32: 1) : String: demo@email.com") != -1);
            #endif

            // Execute CustomerExists
            service.CustomerExists("username");
            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf("ICustomerService.CustomerExists(String: username) : Boolean: True") != -1);
            #endif

            // Execute GetCustomerID
            service.GetCustomerID("username");
            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf("ICustomerService.GetCustomerID(String: username) : Int32: 1") != -1);
            #endif

            // Execute ValidateCustomer
            service.ValidateCustomer("username", "password");
            string logMsg = "ICustomerService.ValidateCustomer(String: username, String: password) : ";
            logMsg += "Customer: { CustomerID: 1, Username: username, Password: password, Email: demo@email.com, Code: None }";
            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf(logMsg) != -1);
            #endif
        }

        [TestMethod]
        public void Validate_list_and_dictionary_objects()
        {
            // Initialize LogHolder
            var holder = new LogHolder();

            // Create dynamic proxy class that includes the debug interceptor
            var service = DebugFactory.CreateProxy<ICustomerService>(new CustomerService());

            // Create Dictionary
            IDictionary<string, string> filters = new Dictionary<string, string>();
            filters.Add("CustomerID", "1");
            filters.Add("Username", "username");
            service.FilterCustomers(filters);

            // Validate
            string logMsg = "ICustomerService.FilterCustomers([(CustomerID)String: 1, (Username)String: username])";
            logMsg += " : [Customer: { CustomerID: 1, Username: username, Password: password, Email: demo@email.com, Code: None },";
            logMsg += " Customer: { CustomerID: 1, Username: username, Password: password, Email: demo@email.com, Code: None, ";
            logMsg += "[Address: { Country: USA, City: Miami, State: Florida }, Address: { Country: PA, City: Panama, ";
            logMsg += "State: Panama }]}]";

            #if DEBUG
                Assert.IsTrue(holder.GetLastLog().IndexOf(logMsg) != -1);
            #endif
        }
    }
}
